/**
 * Created by Lwang on 2017/3/11.
 */
import {square, ajax} from './resize.js';
import {way} from "./common.js";
import $ from "jquery";
import layer from "./layer.js";
import * as async from "async";
square(document, window);

//获取openid
function getOpenid(callback) {
    var _url = ajax.openId;
    if (navigator.userAgent.toLowerCase().match(/MicroMessenger/gi) != null) {
        var XMLHttpReq = new XMLHttpRequest();
        XMLHttpReq.open("GET", _url, true);
        XMLHttpReq.withCredentials = true;
        XMLHttpReq.onreadystatechange = function () {
            if (XMLHttpReq.readyState == 4) {
                if (XMLHttpReq.status == 200) {
                    var _json = XMLHttpReq.response;
                    var openId = _json;
                    if (!openId) {
                        layer.open({
                            type:1,
                            content:"请刷新页面，页面信息已过期!",
                            time:2
                        });
                    } else {
                        way._typeOf(callback) == "[object function]" && callback(openId);
                    }
                } else {
                    throw new Error("XMLHttpRequest response filed!!!!!");
                }
            }
        };
        XMLHttpReq.send(null);
    }
}

$(function () {
    /**
     * 刷新图片验证码
     * @param obj
     */
    $("#changeImg").click(function () {
        $(this).attr("src", "http://api.studypointshare.com/images/kaptcha.jpg");
    });

//    注册
    $("#to-register").click(function () {
        getOpenid(function () {
            var phone = $("#phone").val(),
                psw = $("#psw").val(),
                code = $("#code").val();
            if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
                layer.open({
                    type: 1,
                    content: "请输入手机号码注册!",
                    time: 2
                });
                return;
            }
            async.waterfall([(callback) => {
                $.ajax({
                    url: ajax.exists(phone),
                    xhrFields: {
                        withCredentials: true
                    },
                    success: (res) => {
                        console.log(res);
                        if (!res) {
                            callback();
                        } else {
                            layer.open({
                                type: 1,
                                content: "您的账号已经被注册过!",
                                time: 2
                            })
                        }
                    }
                })
            },
                (cb) => {
                    $.ajax({
                        url: ajax.register,
                        type: "POST",
                        processData: false,
                        "contentType": "application/json; charset=utf-8",
                        xhrFields: {
                            withCredentials: true
                        },
                        data: JSON.stringify({
                            kaptcha: code,
                            name: phone,
                            password: psw
                        }),
                        success: (res) => {
                            if (res.code) {
                                cb(null, res.message)
                            } else {
                                layer.open({
                                    type: 1,
                                    content: "恭喜注册成功",
                                    time: 2,
                                    success: () => {
                                        location.href = "/index.html"
                                    }
                                })
                            }
                        }
                    })
                }
            ], (err, message) => {
                layer.closeAll();
                layer.open({
                    type: 1,
                    content: message,
                    time: 2
                });
            })
        });
    });

//    登录
    $("#j-login").click(() => {
        var phone = $("#phone").val(),
            psw = $("#psw").val();
        $.ajax({
            url: ajax.userLogin,
            type: "POST",
            xhrFields: {
                withCredentials: true
            },
            data: {
                name: phone,
                password: psw
            },
            success: (res) => {
                if (res.code) {
                    layer.open({
                        type: 1,
                        content: res.message,
                        time: 2
                    });
                } else {
                    layer.open({
                        type: 1,
                        content: "恭喜登录成功",
                        time: 2,
                        success: () => {
                            location.href = "/index.html"
                        }
                    })
                }
            }
        })
    });
//    修改密码
    $("#j-modify").click(() => {
        var phone = $("#phone").val(),
            befPsw = $("#befPsw").val(),
            aftPsw = $("#aftPsw").val();
        // code = $("#code").val();
        if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
            layer.open({
                type: 1,
                content: "请输入手机号码注册!",
                time: 2
            });
            return;
        }

        $.ajax({
            url: ajax.register,
            type: "POST",
            processData: false,
            "contentType": "application/json; charset=utf-8",
            xhrFields: {
                withCredentials: true
            },
            data: JSON.stringify({
                teacherId: phone,
                oldPassword: befPsw,
                newPassword: aftPsw
            }),
            success: (res) => {
                if (res.code) {
                    layer.open({
                        type: 1,
                        content: res.message,
                        time: 2
                    })
                } else {
                    layer.open({
                        type: 1,
                        content: "恭喜密码已经修改成功",
                        time: 2,
                        success: () => {
                            location.href = "/index.html"
                        }
                    })
                }
            }
        })
    })
});

